package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.TbLetter;
import com.hqyj.dao.TbLetterMapper;
import com.hqyj.pojo.TbLetterEmail;
import com.hqyj.service.TbLetterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 信件表 服务实现类
 * </p>
 *
 * @author gp
 * @since 2021-09-25
 */
@Service
public class TbLetterServiceImpl implements TbLetterService {
    @Autowired(required = false)
    TbLetterMapper tbLetterMapper;

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public HashMap<String, Object> add(TbLetter tbLetter) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = tbLetterMapper.insert(tbLetter);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "新增失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(TbLetter tbLetter) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = tbLetterMapper.updateById(tbLetter);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "新增失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = tbLetterMapper.deleteById(id);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> list(TbLetter tbLetter) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //第一步：创建分页对象
        Page<TbLetter> tbActivityPage = new Page<>(tbLetter.getPage(), tbLetter.getRow());

        //创建查询条件对象
        QueryWrapper<TbLetter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(
                tbLetter.getCommunityName() != null && !tbLetter.getCommunityName().equals(""),
                "community_name",
                tbLetter.getCommunityName());

        //第二步：查询分页，获取分页数据集
        Page<TbLetter> pageList = tbLetterMapper.selectPage(tbActivityPage, queryWrapper);
        //获取当前页的集合
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
        return map;
    }

    @Override
    public HashMap<String, Object> letterFrom(String ownerName) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<TbLetter> tbLetterList = tbLetterMapper.letterFrom(ownerName);
        map.put("info", tbLetterList);
        return map;
    }

    @Override
    public HashMap<String, Object> sendEmail(TbLetterEmail email) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(email.getTo());
        mailMessage.setFrom(from);
        mailMessage.setSubject(email.getSubject());
        mailMessage.setText(email.getContext());
        javaMailSender.send(mailMessage);
        map.put("info", "邮件发送成功");
        return map;
    }
}
